use actix_session::Session;
use actix_web::{error::ErrorInternalServerError, web, Responder, Result};

use crate::{database::DbPool, route::form::user::LoginForm, service::class};

pub fn config_api_user(cfg: &mut web::ServiceConfig) {
    cfg.service(web::scope("/user").route("/login", web::post().to(login)));
}

async fn login(
    pool: web::Data<DbPool>,
    session: Session,
    form: web::Json<LoginForm>,
) -> Result<impl Responder> {
    class::user::login(pool, session, form.into_inner())
        .await
        .map_err(ErrorInternalServerError)?;
    Ok(web::Json(1))
}
